/*
 * Copyright (c) 2016. jiiiiiin 15398699939@163.com
 */

package com.bankscene.screenfit.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.adapters.ViewPagerAdapter;
import com.bankscene.screenfit.model.Menus;
import com.bankscene.screenfit.util.Stack;
import com.bankscene.screenfit.dict.ErrorDict;
import com.bankscene.screenfit.exception.PresenterModeException;
import com.bankscene.screenfit.fragment.MenusListViewFragment;
import com.bankscene.screenfit.handler.BackHandlerHelper;
import com.bankscene.screenfit.util.ActivityUtils;
import com.bankscene.screenfit.vus.MenusTabsVu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.bankscene.screenfit.activity.MainActivity.MENU_ITEM;
import static com.bankscene.screenfit.activity.MenusActivity.LEFT_FRAGMENT;

public class MenusTabsActivity extends AbstractMenusActivity<MenusTabsVu> {

    private Logger log = LoggerFactory.getLogger(MenusTabsActivity.class);
    private ViewPagerAdapter adapter;
    private Stack<Menus> chooseMenus;

    @Override
    protected void onBindVu() {
        super.onBindVu();
        //设置viewPage
        adapter = ViewPagerAdapter.newInstance(chooseMenu, getSupportFragmentManager());
        vu.setViewPagerAdapter(adapter);
        chooseMenus = new Stack<>();
        chooseMenus.push(chooseMenu);
    }

    @Override
    protected Class<MenusTabsVu> getVuClass() {
        return MenusTabsVu.class;
    }

    @Override
    public final void navigationItemSelectedDo(@NonNull Menus menus) {
        if (menus.equals(chooseMenu)) {
            Toast.makeText(MenusTabsActivity.this, R.string.doagain, Toast.LENGTH_LONG).show();
        } else {
            chooseMenu = menus;
            chooseMenus.push(chooseMenu);
            setTitle(chooseMenu.getTitle());
            Bundle paramsBundle = new Bundle();
            paramsBundle.putParcelable(MENU_ITEM, chooseMenu);
            if (!TextUtils.isEmpty(chooseMenu.getClassName())) {
                paramsBundle.putString(LEFT_FRAGMENT, MenusListViewFragment.class.getName());
                MenusActivity.newInstance(MenusTabsActivity.this, paramsBundle);
                finish();
            } else if (chooseMenu.getFragments() != null && chooseMenu.getFragments().length > 0) {
                adapter.updateBy(chooseMenu);
                //次之:
//                MenusTabsActivity.newInstance(MenusTabsActivity.this, paramsBundle);
//                finish();
            } else {
                switch (menus.getId()) {
                    case R.id.logout_navigation_menu_item:
                        Snackbar.make(vu.getRootView(), getResources().getText(R.string.logout_q), Snackbar.LENGTH_LONG).setAction(getResources().getText(R.string.confirm), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ActivityUtils.logout(activityStackManager);
                            }
                        }).show();
                        break;
                    default:
                        throw new PresenterModeException(ErrorDict.ERROR_SELETEDMENUS_NOTBENULL);
                }
            }
            //不要编码
        }
    }

    @Override
    public boolean handleBackPressed() {
        log.debug("a handleBackPressed {} {}", chooseMenus.size(), fm.getBackStackEntryCount());
        if (chooseMenus.size() <= 1) {
            return BackHandlerHelper.handleBackPress(this);
        } else {
            Menus poped = chooseMenus.pop();
            chooseMenu = chooseMenus.peek();
            log.debug("chooseMenus {} poped:{} chooseMenu:{}", chooseMenus.size(), poped, chooseMenu);
            adapter.updateBy(chooseMenu);
        }
        return true;
    }

    public static void newInstance(Context packageContext, Bundle paramsBundle) {
        Intent intent = new Intent(packageContext, MenusTabsActivity.class);
        if (null != paramsBundle) {
            intent.putExtras(paramsBundle);
        }
        packageContext.startActivity(intent);
    }

}
